package com.group.eams.controller.student;

import com.group.eams.entity.Employee;
import com.group.eams.entity.Student;
import com.group.eams.service.IStudentService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Doctor Who on 2016/11/2.
 */
@Controller
@RequestMapping("/student/student")
public class StudentPoolController {
    private IStudentService studentService;

    @Autowired
    public StudentPoolController(IStudentService studentService){
        this.studentService = studentService;
    }

    /**
     * 列出学员池中的所有学员
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model){
        List<Student> studentList = studentService.getAllStudentList();
        model.addAttribute("list",studentList);
        return "/student/student/listStudent";
    }

    /**
     * 根据姓名查询
     * @param student
     * @param model
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public String queryByName(Student student, Model model){
        List<Student> studentList = studentService.getStudentList(student.getName());
        model.addAttribute("list",studentList);
        return "/student/student/listStudent";
    }

    @RequestMapping(value = "/toAdd")
    public String add(){
        return "/student/student/addStudent";
    }

    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public String addStudent(Student student, Model model){
        if (student == null){
            student = new Student();
        }
        student.setStudentMark(0);
        boolean mark = studentService.addStudent(student);
        if (mark){
            model.addAttribute("info", "添加学员成功！");
        }else{
            model.addAttribute("info","添加学员失败！");
        }
        return list(model);
    }

    @RequestMapping(value = "/toChange")
    public String change(Integer id,Model model){
        Student student = studentService.getStudent(id);
        model.addAttribute("student",student);
        return "/student/student/updateStudent";
    }

    @RequestMapping(value = "/updateStudent",method = RequestMethod.GET)
    public String updateStudent(Student student, Model model){
        if (student == null){
            student = new Student();
        }
        student.setStudentMark(0);
        boolean mark = studentService.updateStudent(student);
        if (mark){
            model.addAttribute("info", "修改学员成功！");
        }else{
            model.addAttribute("info","修改学员失败！");
        }
        return list(model);
    }

    @RequestMapping(value = "/delStudent",method = RequestMethod.GET)
    public String delStudent(Student student, Integer id, Model model){
        if (student == null){
            student = new Student();
        }
        student.setId(id);
        student.setStudentMark(-1); //-1表示无效，0表示未分配，1表示已分配，2表示已报名
        boolean mark = studentService.delStudent(student);
        if (mark){
            model.addAttribute("info", "删除学员成功！");
        }else{
            model.addAttribute("info","删除学员失败！");
        }
        return list(model);
    }

    @RequestMapping(value = "/receiveStudent",method = RequestMethod.GET)
    public String receiveStudent(Student student, Model model,
                                 HttpServletRequest request){
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("empolyee");
        if (student == null){
            student = new Student();
        }
        if (employee != null){
            student.setEmployeeId(employee.getId());
        }
        student.setStudentMark(1);//已分配
        boolean mark = studentService.addStudent(student);
        if (mark){
            model.addAttribute("info", "领取学员成功！");
        }else{
            model.addAttribute("info","领取学员失败！");
        }
        return queryByName(null, model);
    }
}
